package servlet;

import pojo.Function;
import pojo.User;
import service.LoginService;
import service.serviceImpl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017-11-20.
 */
public class BaseServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest requst,HttpServletResponse response) throws ServletException, IOException {
        try {
            //先判断用户是否登录
            //获取字节码对象
            Class clazz = this.getClass();
            //得到方法名称
            String m= requst.getParameter("method");
            if (m==null){
                m="home";
            }
            Method method=clazz.getMethod(m,HttpServletRequest.class,HttpServletResponse.class);
            String s= (String) method.invoke(this,requst,response);
            //判定用户是否有session
            HttpSession session=requst.getSession();
            User user=(User)session.getAttribute("userInfo");

            if (user==null){
                requst.getRequestDispatcher("jsp/index.jsp").forward(requst,response);
            }
            if (s!=null){
                requst.getRequestDispatcher(s).forward(requst,response);
            }
        } catch (Exception e) {
            requst.getRequestDispatcher("/jsp/500.jsp").forward(requst,response);
        }
    }
}
